package com.aiyige.page.publish.selectprice.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.aiyige.utils.ListUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2018/4/8.
 */
public class PriceGroup implements Parcelable {
    String title;
    List<PriceItem> priceItemList;
    double lower;
    double upper;
    boolean selected;

    public PriceGroup() {
    }

    private PriceGroup(Builder builder) {
        setTitle(builder.title);
        setPriceItemList(builder.priceItemList);
        setLower(builder.lower);
        setUpper(builder.upper);
        setSelected(builder.selected);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<PriceItem> getPriceItemList() {
        return priceItemList;
    }

    public void setPriceItemList(List<PriceItem> priceItemList) {
        this.priceItemList = priceItemList;
    }

    public double getLower() {
        return lower;
    }

    public void setLower(double lower) {
        this.lower = lower;
    }

    public double getUpper() {
        return upper;
    }

    public void setUpper(double upper) {
        this.upper = upper;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public void sort() {
        if (ListUtil.isEmpty(priceItemList)) return;
        Collections.sort(priceItemList, new Comparator<PriceItem>() {
            @Override
            public int compare(PriceItem o1, PriceItem o2) {
                double diff = o1.getPriceValue() - o2.getPriceValue();
                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
    }


    public boolean inRange(double price) {
        return price >= lower && price <= upper;
    }


    public static final class Builder {
        private String title;
        private List<PriceItem> priceItemList;
        private double lower;
        private double upper;
        private boolean selected;

        private Builder() {
            selected = false;
            priceItemList = new LinkedList<>();
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder priceItemList(List<PriceItem> priceItemList) {
            this.priceItemList = priceItemList;
            return this;
        }

        public Builder lower(double lower) {
            this.lower = lower;
            return this;
        }

        public Builder upper(double upper) {
            this.upper = upper;
            return this;
        }

        public Builder selected(boolean selected) {
            this.selected = selected;
            return this;
        }

        public PriceGroup build() {
            return new PriceGroup(this);
        }
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.title);
        dest.writeTypedList(this.priceItemList);
        dest.writeDouble(this.lower);
        dest.writeDouble(this.upper);
        dest.writeByte(this.selected ? (byte) 1 : (byte) 0);
    }

    protected PriceGroup(Parcel in) {
        this.title = in.readString();
        this.priceItemList = in.createTypedArrayList(PriceItem.CREATOR);
        this.lower = in.readDouble();
        this.upper = in.readDouble();
        this.selected = in.readByte() != 0;
    }

    public static final Creator<PriceGroup> CREATOR = new Creator<PriceGroup>() {
        @Override
        public PriceGroup createFromParcel(Parcel source) {
            return new PriceGroup(source);
        }

        @Override
        public PriceGroup[] newArray(int size) {
            return new PriceGroup[size];
        }
    };
}
